<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<div id="qesGrid"></div>
<div id="qesToolbar">
	<div>
		<a href="#" class="easyui-linkbutton" onclick="qes.toolbar.showAddDialog();" data-options="
		iconCls:'icon-add',
		plain:true">
			新增
		</a>
		<a href="#" class="easyui-linkbutton" onclick="qes.toolbar.showUpdateDialog();" data-options="
		iconCls:'icon-edit',
		plain:true">
			修改
		</a>
		<a href="#" class="easyui-linkbutton" onclick="qes.init();" data-options="
		iconCls:'icon-edit',
		plain:true">
			显示有效试题
		</a>
		<a href="#" class="easyui-linkbutton" onclick="qes.initclo();" data-options="
		iconCls:'icon-edit',
		plain:true">
			显示无效试题
		</a>
	</div>
	<div>
		<strong>关键字：</strong>
		<input type="text" name="searchName" id="searchName" class="easyui-textbox"/>
		
		<strong>科目：</strong>
		<select name="role" id="cousreSearch" style="width:150px">
		</select>
		
		<strong>题型：</strong>
		<select name="state" class="easyui-combobox" id="typeSearch" style="width:100px">
			<option value="">全部</option>
			<option value="1">单选</option>
			<option value="2">多选</option>
			<option value="3">判断</option>
		</select>
		<strong>难度：</strong>
		<select name="state" class="easyui-combobox" id="difSearch" style="width:100px">
			<option value="">全部</option>
			<option value="1">低</option>
			<option value="2">中</option>
			<option value="3">较高</option>
			<option value="4">高</option>
		</select>
		<a href="#" onclick="qes.toolbar.search();" class="easyui-linkbutton" data-options="
		iconCls:'icon-search',
		plain:true">
			查询
		</a>
	</div>
</div>
<div id="qesAddDialog" class="easyui-dialog ec-dialog" data-options="
title:'新增菜单',
buttons:'#qesAddButtons',
closed: true,
onClose:qes.toolbar.resetAddFrom">
	<form id="qesAddForm" method="post">
		<p>
			<strong>所属科目：</strong>
			<input type="text" name="course" id="course"class="easyui-textbox">
		</p>
		<p>
			<strong>题&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;型：</strong>
			<select name="type" class="easyui-combobox" id="type" style="width:100px">
			<option value="1">单选</option>
			<option value="2">多选</option>
			<option value="3">判断</option>
		</select>
		<p>
			<strong>难&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;度：</strong>
			<select name="difficulty" class="easyui-combobox" id="difficulty" style="width:100px">
			<option value="1">低</option>
			<option value="2">中</option>
			<option value="3">较高</option>
			<option value="4">高</option>
		</select>
		</p>
		<p>
			<strong>题&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;目：</strong>
			<input type="text" name="question" id="question" class="easyui-textbox" data-options="
			required:true">
		</p>
		<p>
			<strong>选&nbsp;&nbsp;&nbsp;项&nbsp;&nbsp;A：</strong>
			<input type="text" name="a" id="a" class="easyui-textbox" data-options="
			required:true">
		</p>
		<p>
			<strong>选&nbsp;&nbsp;&nbsp;项&nbsp;&nbsp;B：</strong>
			<input type="text" name="b" id="b" class="easyui-textbox" data-options="
			required:true">
		</p>
		<p>
			<strong>选&nbsp;&nbsp;&nbsp;项&nbsp;&nbsp;C：</strong>
			<input type="text" name="c" id="c" class="easyui-textbox" data-options="
			required:true">
		</p>
		<p>
			<strong>选&nbsp;&nbsp;&nbsp;项&nbsp;&nbsp;D：</strong>
			<input type="text" name="d" id="d" class="easyui-textbox" data-options="
			required:true">
		</p>
		<p>
			<strong>答&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;案：</strong>
			<input type="text" name="answer" id="answer" class="easyui-textbox" data-options="
			required:true">
		</p>
	</form>
</div>
<div id="qesAddButtons">
	<a href="#" onclick="qes.toolbar.add()" class="easyui-linkbutton" data-options="
	iconCls: 'icon-add',
	plain: true">
		新增
	</a>
	<a href="#" onclick="qes.toolbar.hiddenAddDialog();" class="easyui-linkbutton" data-options="
	iconCls: 'icon-redo',
	plain: true">
		取消
	</a>
</div>
<div id="qesUpdateDialog" class="easyui-dialog ec-dialog" data-options="
title:'修改菜单信息',
buttons:'#qesUpdateButtons',
closed: true,
onClose:qes.toolbar.resetUpdateFrom">
	<form id="qesUpdateForm" method="post">
	<input name="id" type="hidden"/>
		<p>
			<strong>所属科目：</strong>
			<input type="text" name="course" id="course"class="easyui-textbox"disabled="disabled">
		</p>
		<p>
			<strong>题&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;型：</strong>
			<select name="type" class="easyui-combobox" id="type" style="width:100px">
			<option value="1">单选</option>
			<option value="2">多选</option>
			<option value="3">判断</option>
		</select>
		<p>
			<strong>难&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;度：</strong>
			<select name="difficulty" class="easyui-combobox" id="difficulty" style="width:100px">
			<option value="1">低</option>
			<option value="2">中</option>
			<option value="3">较高</option>
			<option value="4">高</option>
		</select>
		</p>
		<p>
			<strong>题&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;目：</strong>
			<input type="text" name="question" id="question" class="easyui-textbox" data-options="
			required:true">
		</p>
		<p>
			<strong>选&nbsp;&nbsp;&nbsp;项&nbsp;&nbsp;A：</strong>
			<input type="text" name="a" id="a" class="easyui-textbox" data-options="
			required:true">
		</p>
		<p>
			<strong>选&nbsp;&nbsp;&nbsp;项&nbsp;&nbsp;B：</strong>
			<input type="text" name="b" id="b" class="easyui-textbox" data-options="
			required:true">
		</p>
		<p>
			<strong>选&nbsp;&nbsp;&nbsp;项&nbsp;&nbsp;C：</strong>
			<input type="text" name="c" id="c" class="easyui-textbox" data-options="
			required:true">
		</p>
		<p>
			<strong>选&nbsp;&nbsp;&nbsp;项&nbsp;&nbsp;D：</strong>
			<input type="text" name="d" id="d" class="easyui-textbox" data-options="
			required:true">
		</p>
		<p>
			<strong>答&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;案：</strong>
			<input type="text" name="answer" id="answer" class="easyui-textbox" data-options="
			required:true">
		</p>
	</form>
</div>
<div id="qesUpdateButtons">
	<a href="#" onclick="qes.toolbar.update(id)" class="easyui-linkbutton" data-options="
	iconCls: 'icon-edit',
	plain: true">
		修改
	</a>
	<a href="#" onclick="qes.toolbar.hiddenUpdateDialog();" class="easyui-linkbutton" data-options="
	iconCls: 'icon-redo',
	plain: true">
		取消
	</a>
</div>
<script>
	 var qes={
			 init : function() {
				 	qes.qesGrid = $('#qesGrid');
				 	qes.qesAddDialog = $('#qesAddDialog');
				 	qes.qesAddForm = $('#qesAddForm');
				 	qes.qesUpdateForm = $('#qesUpdateForm');
				 	qes.qesUpdateDialog = $('#qesUpdateDialog');
				 	qes.course = $('#course');
				 	qes.cousreSearch= $('#cousreSearch');
				 	qes.searchName= $('#searchName');
				 	qes.stateSearch= $('#stateSearch');
				 	qes.typeSearch= $('#typeSearch');
				 	qes.difSearch= $('#difSearch');
					
				 	qes.qesGrid.datagrid({
					    url: '<%=request.getContextPath()%>/que/QestionsServlet?command=listQestionsOp',
					    fitColumns: true,
					    striped: true,
					    rownumbers: true,
					    pagination: true,
					    pageSize: 10,
					    pageList: [10, 20, 30, 40, 50],
					    pageNumber: 1,
					    toolbar: '#qesToolbar',
					    columns:[[
							{field:'id', width:100, checkbox: true},
							{field:'name', title:'科目', width:100, align:'center'},
							{field:'questionTypeStr', title:'题型', width:100, align:'center'},
							{field:'question', title:'题目', width:100, align:'center'},
							{field:'difficultyStr', title:'难度', width:100, align:'center'},
							{field:'operateTime', title:'操作时间', width:100, align:'center'},
							{field:'stateStr', title:'状态', width:100, align:'center'},
							{field:'StateOp', title:'操作', formatter: qes.row.showGender, width:100, align:'center'}
					    ]],
						onDblClickRow: function(rowIndex, rowData) {
							qes.toolbar.showUpdateDialog(rowData.id);
						},
						onBeforeLoad: function(param){
								var name= $('#name').val();
								var time= $('#time').val();
								param.name=name;
								param.time=time;
						}
					});
				},
				
				initclo : function() {
					qes.qesGrid = $('#qesGrid');
					qes.qesAddDialog = $('#qesAddDialog');
					qes.qesAddForm = $('#qesAddForm');
					qes.qesUpdateForm = $('#qesUpdateForm');
					qes.qesUpdateDialog = $('#qesUpdateDialog');
					qes.course = $('#course');
					qes.cousreSearch= $('#cousreSearch');
					qes.searchName= $('#searchName');
					qes.stateSearch= $('#stateSearch');
				 	qes.typeSearch= $('#typeSearch');
				 	qes.difSearch= $('#difSearch');
					
					qes.qesGrid.datagrid({
					    url: '<%=request.getContextPath()%>/que/QestionsServlet?command=listQestionsClo',
					    fitColumns: true,
					    striped: true,
					    rownumbers: true,
					    pagination: true,
					    pageSize: 10,
					    pageList: [10, 20, 30, 40, 50],
					    pageNumber: 1,
					    toolbar: '#qesToolbar',
					    columns:[[
							{field:'id', width:100, checkbox: true},
							{field:'name', title:'科目', width:100, align:'center'},
							{field:'questionTypeStr', title:'题型', width:100, align:'center'},
							{field:'question', title:'题目', width:100, align:'center'},
							{field:'difficultyStr', title:'难度', width:100, align:'center'},
							{field:'operateTime', title:'操作时间', width:100, align:'center'},
							{field:'userName', title:'操作者', width:100, align:'center'},
							{field:'stateStr', title:'状态', width:100, align:'center'},
							{field:'StateOp', title:'操作', formatter: qes.row.showGender, width:100, align:'center'}
					    ]],
						onDblClickRow: function(rowIndex, rowData) {
							qes.toolbar.showUpdateDialog(rowData.id);
						},
						onBeforeLoad: function(param){
								var name= $('#name').val();
								var time= $('#time').val();
								param.name=name;
								param.time=time;
						}
					});
				},
				
				showcousreSearchDialog: function() {
					qes.cousreSearch.combotree({
						url: '<%=request.getContextPath()%>/CourseServlet?command=listMenu',
						multiple: false,
						checkbox: true,
						onlyLeafCheck: false,
						onLoadSuccess: function(node, data) {
							$(this).tree('expandAll')
						}
					});
				},
				
				toolbar: {
					showAddDialog: function() {
						qes.course.combotree({
							url: '<%=request.getContextPath()%>/CourseServlet?command=listMenu',
							multiple: false,
							checkbox: true,
							onlyLeafCheck: false,
							onLoadSuccess: function(node, data) {
								$(this).tree('expandAll')
							}
						});
						qes.qesAddDialog.dialog('open');
					},
					
					
					hiddenAddDialog: function() {
						$('#qesAddDialog').dialog('close');
						qes.toolbar.resetAddForm();
						qes.qesGrid.datagrid('reload');
					},
					resetAddForm: function() {
						qes.qesAddForm.form('reset');
					},
					add: function() {
						if (qes.qesAddForm.form('validate')) {
							$.messager.progress({
								text : '正在处理...'
							});
						
						$.post('<%=request.getContextPath()%>/que/QestionsServlet?command=addQue',$("#qesAddForm").serialize(), function(data) {
							if (data) {
								$.messager.show({
									title: '系统提示',
									msg: '新增成功！',
									timeout: 3000,
									showType: 'slide'
								});
								
							} else {
								$.messager.alert('系统提示', '新增失败！', 'error')
							}
							$.messager.progress('close');
							qes.toolbar.hiddenAddDialog();
						});
					}
				},
					
					//显示修改界面
					showUpdateDialog: function(id) {
						var getData = function(v) {
							
							$.post('<%=request.getContextPath()%>/que/QestionsServlet?command=getQue', {id : v}, function(data) {
								console.info(data);
								if (data) {
									qes.qesUpdateForm.form('load', {
										id:data.id,
										course:data.name,
										question:data.question,
										a:data.answerA,
										b:data.answerB,
										c:data.answerC,
										d:data.answerD,
										answer:data.answer,
										difficulty:data.difficultyStr,
										type:data.questionTypeStr,
									});
									$('#qesUpdateDialog').dialog('open');
								} else {
									$.messager.alert('系统提示', '获取信息失败！', 'error')
								}
								$.messager.progress('close');
							}, 'json');
						}
						if (id) {
							getData(id);
						} else {
							var row = qes.qesGrid.datagrid('getSelections');
							if (row.length == 1) {
								getData(row[0].id);
							} else {
								$.messager.show({
									title: '系统提示',
									msg: '请选择一行数据进行修改！',
									timeout: 3000,
									showType: 'slide'
								});	
							}
						}
					},
					
					hiddenUpdateDialog: function() {
						$('#qesUpdateDialog').dialog('close');
						qes.qesGrid.datagrid('reload');
					},
					resetUpdateFrom: function() {
						qes.qesUpdateForm.form('reset');
					},
					
					update: function() {
						if (qes.qesUpdateForm.form('validate')) {
							$.messager.progress({
								text : '正在处理...'
							});
							$.post('<%=request.getContextPath()%>/que/QestionsServlet?command=updateQue', $("#qesUpdateForm").serialize(), function(data) {
								if (data == 1) {
									$.messager.show({
										title: '系统提示',
										msg: '修改成功！',
										timeout: 3000,
										showType: 'slide'
									});
									qes.toolbar.resetUpdateFrom();
									qes.qesGrid.datagrid('reload');
								} else {
									$.messager.alert('系统提示', '修改失败！', 'error');
								}
								$.messager.progress('close');
								qes.toolbar.hiddenUpdateDialog();
							});
							
							
						}
					},
					redo: function() {
						qes.qesGrid.datagrid('unselectAll');
					},
					search: function(){
						console.info(qes.searchName.val());
						var cousreSearch = $("#cousreSearch").combobox("getValue");
						var typeSearch = $("#typeSearch").combobox("getValue");
						var difSearch = $("#difSearch").combobox("getValue");
						 qes.qesGrid.datagrid('load',{
							SearchName: qes.searchName.val(),
							cousreSearch:cousreSearch,
							typeSearch:typeSearch,
							difSearch:difSearch,
						});
						
					} 
				},
				
				row : {
					showGender: function(value, row) {
						var id = row.id;
						var state = row.state;
						if (!value) {
						 value = state == '1' ? '禁用' : '启用';
						}
						var operations = [];
						operations.push('<a href="javascript:void(0)" onclick="qes.row.updateGender(\'' + id + '\',  \'' + value + '\')">' + value + '</a>');
						return operations.join('');
					},
					updateGender: function(id, value) {
						$.messager.confirm('Confirm', '你确定要更改状态吗?', function(result){
							if (result){
								
								 $.ajax({   
						                url: '<%=request.getContextPath()%>/que/QestionsServlet?command=updateState' + "&id=" + id + "&value=" + value,//传参数到一般处理方法，并赋值  
						                 
						            type:"Post",  
						                dataType:"json",  
						                success: function (data) {//ajax请求完成时执行，data为返回的结果  
						                	$.messager.show({
												title: '系统提示',
												msg: '状态修改成功！',
												timeout: 3000,
												showType: 'slide'
											});
						                	qes.qesGrid.datagrid('reload');
						                }  
						            });  
								
								
							} else {
								$.messager.alert('系统提示', '状态修改失败！', 'error')
							}
						});
					}
				} 
			};
	 
			
			$(function(){
				qes.init();
				qes.showcousreSearchDialog();
			});
			
</script>